import {
	msg
} from "./wxAPI";
export const pays = (info, callback, errorCallback) => {
	if (typeof WeixinJSBridge == 'undefined') {
		if (document.addEventListener) {
			document.addEventListener('WeixinJSBridgeReady', onBridgeReady(info, callback, errorCallback), false);
		} else if (document.attachEvent) {
			document.attachEvent('WeixinJSBridgeReady', onBridgeReady(info, callback, errorCallback));
			document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(info, callback, errorCallback));
		}
	} else onBridgeReady(info, callback, errorCallback);
}

function onBridgeReady(info, callback, errorCallback) {
	WeixinJSBridge.invoke(
		'getBrandWCPayRequest',
		info,
		function(res) {
			if (res.err_msg == 'get_brand_wcpay_request:ok') {
				callback(res);
			} else {
				msg('支付失败');
				errorCallback()
			}
		},
		function(err) {
			msg('支付失败');
			errorCallback()
		}
	);
}